﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using SimulationCore.Net.DTO;
using SimulationPresenter.Presentation.Helpers;
using SimulationPresenter.Presentation.Primitives;
using SimulationPresenter.Simulation;
using SimulationPresenter.Simulation.Primitive;

namespace SimulationPresenter.Theater.Actors
{
    internal class RidigMeshActor : RidigActorPrimitive, IDrawable
    {
        private IPresentationPrimitive shape;

        public RidigMeshActor(IPresentationPrimitive shape, string name, Vector3 initialPosition, SimulationEnvironment environment)
        {
            this.shape = shape;
            state = new RidigMesh()
                            {
                                BodyShape = shape,
                                Name = name
                            };

            state.Transformation = Matrix.Translation(initialPosition);
            state.PutOrUpdate(environment);

        }

        public void DrawActor(Device device)
        {
            IndexedVerticesDrawer.Draw(shape, state.Transformation, device);
        }

        public void Highlight(Color color)
        {
            //Todo: Some highlighting for textured elements
        }

        public static RidigMeshActor[] FromFile(string fileName, string name, Vector3 initialPosition, SimulationEnvironment environment, Device device)
        {
            var actors = new List<RidigMeshActor>();
            var shapes = MeshShape.FromFile(fileName, device);

            int index = 0;
            foreach (var shape in shapes)
            {
                actors.Add(new RidigMeshActor(shape,
                                                string.Format("{0}-{1}", name, index++), 
                                                initialPosition, 
                                                environment));
            }

            return actors.ToArray();
        }

    }
}
